package com.geeklan.smart.ui.settings;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Toast;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.data.model.User;
import com.geeklan.smart.data.repository.user.AccountRepository;
import com.geeklan.smart.databinding.ActivityUpdatePwdBinding;
import com.geeklan.smart.ui.login.LoginActivity;
import com.geeklan.smart.ui.main.MainActivity;
import com.geeklan.smart.ui.viewmodel.user.UserViewModel;
import com.geeklan.smart.ui.viewmodel.user.UserViewModelFactory;
import com.geeklan.smart.utils.MD5Utils;
import com.geeklan.smart.utils.PasswordValidator;
import com.geeklan.smart.utils.SharedPreferencesUtil;

public class UpdatePwdActivity extends BaseActivity<ActivityUpdatePwdBinding> {
    private UserViewModel userViewModel;
    @Override
    protected ActivityUpdatePwdBinding initBinding() {
        return ActivityUpdatePwdBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        userViewModel = new ViewModelProvider(this, new UserViewModelFactory(new AccountRepository())).get(UserViewModel.class);

        userViewModel.getUpdatePwdLiveData().observe(this, response -> {
            dismiss();
            Toast.makeText(this, R.string.update_pwd_success,Toast.LENGTH_SHORT).show();
            finishAll();
            SharedPreferencesUtil.clear();
            startActivity(new Intent(this, LoginActivity.class));
        });

        userViewModel.getErrorLiveData().observe(this, s -> {
            dismiss();
            Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
        });

        mBinding.tvOk.setOnClickListener(v -> {
            if(!mBinding.etInputPwd.getText().toString().equals(mBinding.etInputPwdAgain.getText().toString())){
                Toast.makeText(this, R.string.again_input_pwd_error,Toast.LENGTH_SHORT).show();
            } else if(!PasswordValidator.isValidPassword(mBinding.etInputPwd.getText().toString())) {
                Toast.makeText(this, R.string.input_password,Toast.LENGTH_SHORT).show();
            }else {
                loading();
                User user = new User();
                user.setNewPassword(MD5Utils.encryptMD5(mBinding.etInputPwd.getText().toString() + "miaomojo"));
                userViewModel.updatePassword(user);
            }
        });

        mBinding.ivBack.setOnClickListener(v -> {
            finish();
        });
        mBinding.ivVisibility.setOnClickListener(v -> {
            if (!isShowPwd) {
                isShowPwd = true;
                mBinding.ivVisibility.setImageResource(R.drawable.login_pwd_show);
                //显示
                mBinding.etInputPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                isShowPwd = false;
                mBinding.ivVisibility.setImageResource(R.drawable.login_pwd_hint);
                //隐藏
                mBinding.etInputPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });

        mBinding.ivVisibility1.setOnClickListener(v -> {
            if (!isShowPwd1) {
                isShowPwd1 = true;
                mBinding.ivVisibility1.setImageResource(R.drawable.login_pwd_show);
                //显示
                mBinding.etInputPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                isShowPwd1 = false;
                mBinding.ivVisibility1.setImageResource(R.drawable.login_pwd_hint);
                //隐藏
                mBinding.etInputPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });
    }
    private boolean isShowPwd = false;
    private boolean isShowPwd1 = false;
    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }
    public static void startActivity(Context context){
        Intent intent = new Intent(context, UpdatePwdActivity.class);
        context.startActivity(intent);
    }
}